package sunlight.core;

import sunlight.database.MySqlDatabase;
import sunlight.logger.EventLogger;
import sunlight.util.config.ConfigurationFile;

public class Awaken {

	protected MySqlDatabase Database; 
	
	public  boolean	hasDababaseConn = false;
	
	public Awaken(){
		
		//---incarca fisierul de configurari-------------//
		ConfigurationFile configuration;

        try {
        	configuration = Application.getConfiguration();

            if(configuration != null){
                //---conectare la baza de date---------//

                String dbHost       = (String) configuration.get("database.host");
                String dbPort       = (String) configuration.get("database.port", "3036");
                String dbUser       = (String) configuration.get("database.username", "root");
                String dbPass       = (String) configuration.get("database.password", "");
                String dbName       = (String) configuration.get("database.name");
                String dbCharset    = (String) configuration.get("database.charset", "utf8");
                String dbEncoding   = (String) configuration.get("database.encoding", "utf-8");

                //----conectare la baza de date------------------------//
                this.Database = new MySqlDatabase(dbHost, Integer.parseInt(dbPort), dbUser, dbPass, dbName, dbCharset, dbEncoding);
                
                if(this.Database.isConnected()){
                	this.hasDababaseConn = true;
                }
                
            }
            else{
            	Application.Log.write("Nu gasesc fisierul de configurari! ", EventLogger.ERROR);
            	Application.exit();
            }
        } catch (NullPointerException nex) {
        	Application.Log.write("Nu gasesc fisierul de configurari! " + nex.toString(), EventLogger.ERROR);
        	Application.exit();
        }
	}
	
}
